package cn.gailvlun.gll.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import org.joda.time.DateTime;

import cn.gailvlun.gll.net.R;
import cn.gailvlun.gll.net.databinding.DialogDatePickerBinding;
import cn.gailvlun.gll.util.DateUtil;



public class DatePickerDialog extends Dialog {

    private DialogDatePickerBinding mBinding;

    public enum Type {
        ALL,
        TIME
    }

    private Type mType = Type.ALL;

    public DatePickerDialog(@NonNull Context context) {
        super(context, R.style.BaseDialog_Bottom);
        mBinding = DataBindingUtil.inflate(getLayoutInflater(), R.layout.dialog_date_picker, null, false);
        mBinding.setPresenter(new Presenter());
    }

    public void setFirstNotComplete(final DateTime startDateTime, DateTime endDateTime) {
        setRangeDate(startDateTime, endDateTime);
        setRangeHourFixMinute(startDateTime.getHourOfDay(), 23, 0);
        mBinding.dp.setOnDayChangeListener(new DatePicker.OnDayChangeListener() {
            boolean firstDay = true;
            @Override
            public void onDayChange(DateTime dateTime) {
                if (DateUtil.isSameDay(startDateTime, dateTime)) {
                    if (!firstDay) {
                        setRangeHourFixMinute(startDateTime.getHourOfDay(), 23, 0);
                    }
                    firstDay = true;
                } else {
                    if (firstDay) {
                        setRangeHourFixMinute(0, 23, 0);
                    }
                    firstDay = false;
                }
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initWindow();

        setContentView(mBinding.getRoot());
    }

    private void initWindow() {
        Window window = getWindow();
        window.requestFeature(Window.FEATURE_NO_TITLE);

        window.setWindowAnimations(R.style.BaseDialog_Bottom_UP);
        window.getDecorView().setPadding(0, 0, 0, 0);

        WindowManager.LayoutParams lp = window.getAttributes();

        lp.width = WindowManager.LayoutParams.MATCH_PARENT;

        lp.gravity = Gravity.BOTTOM;
        window.setAttributes(lp);
    }

    public void setFixDate(DateTime fixDate) {
        mBinding.dp.setFix(fixDate);
    }

    public void setFixTime(DateTime fixTime) {
        mBinding.tp.setFix(fixTime);
    }

    public void setRangeDate(final DateTime startTime, final DateTime endTime) {
        mBinding.dp.setRange(startTime, endTime);
    }

    public void setRangeTime(int startHour, int endHour) {
        mBinding.tp.setRange(startHour, endHour);
    }

    public void setRangeHourFixMinute(int startHour, int endHour, int fixMinute) {
        mBinding.tp.setRangeHourFixMinute(startHour, endHour, fixMinute);
    }

    public void setSelectDate(DateTime dateTime) {
        if (dateTime == null) {
            return;
        }
        mBinding.dp.setSelectDate(dateTime);
    }

    public void setSelectTime(DateTime selectHour) {
        if (selectHour == null) {
            return;
        }
        mBinding.tp.setSelectTime(selectHour.getHourOfDay());
    }

    
    private OnConfirmListener<DatePickerDialog, DateTime> onConfirmListener;

    public void setOnConfirmListener(OnConfirmListener<DatePickerDialog, DateTime> onConfirmListener) {
        this.onConfirmListener = onConfirmListener;
    }

    public void setType(Type type) {
        mType = type;
        switch (type) {
            case ALL: {
                
                break;
            }
            case TIME: {
                mBinding.dp.setVisibility(View.GONE);
                break;
            }
        }
    }

    public void setTitle(CharSequence title) {
        mBinding.tvTitle.setText(title);

    }

    public class Presenter {
        public void onCancel() {
            
            dismiss();
        }

        public void onConfirm() {
            if (onConfirmListener != null) {
                switch (mType) {
                    case ALL: {
                        DateTime dpDate = mBinding.dp.getDate();
                        DateTime tpTime = mBinding.tp.getTime();
                        DateTime dt = new DateTime(dpDate.getYear(), dpDate.getMonthOfYear(), dpDate.getDayOfMonth(),
                                tpTime.getHourOfDay(), tpTime.getMinuteOfHour());
                        onConfirmListener.onConfirm(DatePickerDialog.this, dt);
                        break;
                    }
                    case TIME: {
                        DateTime tpTime = mBinding.tp.getTime();
                        DateTime dt = DateTime.now().withTime(tpTime.getHourOfDay(), tpTime.getMinuteOfHour(),
                                0, 0);
                        onConfirmListener.onConfirm(DatePickerDialog.this, dt);
                        break;
                    }
                }

            }
            dismiss();
        }
    }
}
